package com.example.server.controller;

import com.example.server.entity.Manager;
import com.example.server.service.ManagerService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("manager")
public class ManagerController extends BaseController{

    private final ManagerService managerService;

    public ManagerController(ManagerService managerService) {
        this.managerService = managerService;
    }

    @GetMapping("queryAll")
    @ResponseBody
    private List<Manager> queryAll(){
        return managerService.queryAll();
    }

    @PostMapping("create")
    @ResponseBody
    @CrossOrigin("http://localhost:5173")
    private Manager create(@RequestBody Manager manager){
       managerService.create(manager);

       Manager resultManager = new Manager();

       resultManager.setMid(manager.getMid());
       resultManager.setName(manager.getName());
       resultManager.setEmail(manager.getEmail());
       resultManager.setTelephone(manager.getTelephone());
       resultManager.setPassword(manager.getPassword());
        return resultManager;
    }

    @PostMapping("queryByAccountAndPassword")
    @ResponseBody
    private Manager queryByAccountAndPassword(@RequestBody Map<String,String> map){
         Manager manager = managerService.queryOne("queryByAccountAndPassword",map);
         session.setAttribute("manager",manager);
        return manager;
    }
}
